package datastructure.day01;

/**
 * @author: Gan
 * @date 2021/4/27 0027 19:44
 * @description: 3, 判断一个链表是否有环
 **/
public class Linked2 {
    public static void main(String[] args) {
        LinkNode node1 = new LinkNode("travel");
        LinkNode node2 = new LinkNode("play");
        LinkNode node3 = new LinkNode("game");
        LinkNode node4 = new LinkNode("walk");
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node1;
        LinkNode p1 = node1;//一个首指针
        LinkNode p2 = p1;//标记指针
        while (p1 != null) {
            p1 = p1.next;
            if (p1 == p2) {
                System.out.println("链表有环！");
                return;
            }
        }
        System.out.println("链表没有环。");
    }

}

class LinkNode {
    String value;
    LinkNode next;

    public LinkNode(String value) {
        this.value = value;
    }
}